
/**
 * @param {WebMail} oWebMail
 * @param {WebMailMailScreen} oScreen
 * @return {WebMailTemplateDataBuilder}
 */
WebMailTemplateDataBuilder = function (oWebMail, oScreen)
{
	this.webmail = oWebMail;
	this.screen = oScreen;
	return this;
};

WebMailTemplateDataBuilder.prototype =
{
	buildMessage: function (oMessage)
	{
		var sListItemKey = this.webmail.generateHash({
			folder: oMessage.Folder,
			uid: oMessage.Uid
		});

		return $($.fn.pureJSTemplate({
				id   : 'template-message.html',
				data : {
					Subject: this.encodeHtml(oMessage.Info.Subject),
					From: this.encodeHtml(oMessage.Info.From),
					FromShort: this.encodeHtml(oMessage.Info.FromShort),
					Date: this.encodeHtml(oMessage.Info.Date),
					Size: oMessage.Info.Size,
					HasImages: oMessage.HasImages || false,
					HasPlain: 0 < (oMessage.Plain || '').length,
					HasHtml: 0 < (oMessage.Html || '').length,
					SaveLink: oMessage.Info.SaveLink,
					Attachments: oMessage.Info.Attachments || []
				}
			})).attr('rel', sListItemKey).attr('data-x-tabname', oMessage.Info.TabName);
	},

	buildMessageList: function (oList)
	{
		var oData = { List: [] };

		for (var iKey in oList.Response)
		{
			oData.List.push({
				Rel: this.webmail.generateHash({
					folder: oList.Folder, uid: oList.Response[iKey].Uid
				}),
				Subject: this.encodeHtml(oList.Response[iKey].Subject),
				From: this.encodeHtml(oList.Response[iKey].FromShort),
				Size: oList.Response[iKey].Size,
				Date: oList.Response[iKey].Date,
				HasAttachments: oList.Response[iKey].HasAttachments,
				IsFlagged: oList.Response[iKey].IsFlagged,
				IsUnread: oList.Response[iKey].IsUnread,
				IsAnswered: oList.Response[iKey].IsAnswered
			});
		}

		var sListCounts = '';
		var iPageCount = 1;
		var iPage = parseInt(oList.Page)
		var iCount = parseInt(oList.Count)
		var iMessagePerPage = parseInt(this.webmail.data.Settings.MessagePerPage);

		if (0 < iCount)
		{
			var iStart = ((iPage - 1) * iMessagePerPage) + 1;
			var iEnd = iStart + iMessagePerPage - 1;
			iEnd = (iCount < iEnd) ? iCount : iEnd;
			iPageCount = Math.ceil(iCount / iMessagePerPage);

			sListCounts = iStart + ' ~ ' + iEnd +' / ' + iCount
		}

		var sPages = '';
		if (1 < iPageCount)
		{
			var sFirstPage, sLastPage, sPrevPage, sNextPage;
			sFirstPage = sLastPage = sPrevPage = sNextPage = '';

			var oHash = {
				folder: oList.Folder, search: oList.Search
			}

			if (1 < iPage - 1)
			{
				oHash['page'] = 1;
				sFirstPage = '<span class="page" rel="' + this.webmail.generateHash(oHash) + '">first</span>';
			}

			if (1 < iPage)
			{
				oHash['page'] = iPage - 1;
				sNextPage = '<span class="page" rel="' + this.webmail.generateHash(oHash) + '">next</span>';
			}

			if (iPageCount > iPage + 1)
			{
				oHash['page'] = iPageCount;
				sLastPage = '<span class="page" rel="' + this.webmail.generateHash(oHash) + '">last</span>';
			}

			if (iPageCount > iPage)
			{
				oHash['page'] = iPage + 1;
				sPrevPage = '<span class="page" rel="' + this.webmail.generateHash(oHash) + '">prev</span>';
			}

			sPages = $.trim(sFirstPage + ' ' + sNextPage + ' (' + sListCounts + ') ' + sPrevPage + ' ' + sLastPage).replace('/[ ]+/g', ' ');
		}

		oData.Pages = sPages;

		return $($.fn.pureJSTemplate({
				id   : 'template-list.html',
				data : oData
			})).attr('rel', this.webmail.generateHash({
				folder: oList.Folder, page: oList.Page
			})).attr('data-x-tabname', oList.TabName);
	},

	buildFolderList: function (aFolders, bIsSub)
	{
		bIsSub = IsUnd(bIsSub) ? false : bIsSub;
		var oAddFolderData, oData, iKey;
		oData = {
			List: []
		};
		for (iKey in aFolders)
		{
			oAddFolderData = {
				//Rel: this.webmail.generateHash({ folder: aFolders[iKey].FullName }),
				Rel: this.webmail.generateHash({ folder: aFolders[iKey].DecodedFullName }),
				Name: aFolders[iKey].Name,
				SubFolders: '',
				NoSelect: aFolders[iKey].NoSelect
			};

			if (aFolders[iKey].SubFolders && 0 < aFolders[iKey].SubFolders.length)
			{
				oAddFolderData.SubFolders = this.buildFolderList(aFolders[iKey].SubFolders, true);
			}

			oData.List.push(oAddFolderData);
		}

		return (bIsSub) 
			? $.fn.pureJSTemplate({ id   : 'template-folders.html', data : oData })
			: $($.fn.pureJSTemplate({ id   : 'template-folders.html', data : oData }));
	},

	buildQuotedLink: function ()
	{
		return $($.fn.pureJSTemplate({id: 'template-show-quoted-link.html', data: {}}));
	},

	buildCompose: function ()
	{
		var oAdd = $($.fn.pureJSTemplate({
				id   : 'template-compose.html',
				data : {}
			})).attr('rel', '#compose').attr('data-x-tabname', 'Compose Screen');

		$('textarea.html-editor', oAdd).cleditor()[0].focus();
		
		return oAdd;
	},

	buildTabAdd: function ()
	{
		return $($.fn.pureJSTemplate({ id: 'template-tab-add.html', data : {} }));
	},

	buildTab: function (sRelKey, sTabName)
	{
		return $($.fn.pureJSTemplate({
				id   : 'template-tab.html',
				data : {Rel: sRelKey, Name: sTabName}
			})).addClass('tab').attr('rel', sRelKey);
	},
	
	/**
	 * @param {string} sText
	 * @return {string}
	 */
	encodeHtml: function (sText)
	{
		return sText.replace(/&/g, "&amp;").replace(/</g, "&lt;")
			.replace(/>/g, "&gt;").replace(/"/g, '&quot;').replace(/'/g, '&#039;');
	}
}
